<?php

namespace My\Controller;

use Utuo\BackgroundController;

class ProductController extends BackgroundController {
	
	public function _initialize() {
		$this->setControllerTitle('商城信息设置');
		$this->setActionTitle(array(
				'index' => '商城信息',
				'config' => '商城配置',
				'wechat' => '微信回复设置',
				'url' => '商城地址',
		));
		$this->setActionNav('index,config,wechat,url');
	}
	
	// 商城信息
	public function indexAction() {
		$dProduct = D('Product');
		$product = $dProduct->find($this->aid);
		if (!$product) {
			$data = array(
					'id' => $this->aid,
					'cid' => $this->aid,
					'name' => $this->aname 
			);
			if (!$dProduct->add($data)) {
				$this->error('添加商城信息失败');
			}
			$product = $dProduct->find($this->aid);
		}
		$product['status'] = $this->getRadio('status', $dProduct->status, $product['status']);
		$this->assign('product', $product);
		$this->display('save');
	}
	
	// 首页地址
	public function urlAction() {
		$product = M('product')->find($this->aid);
		$product['url'] = uu($this->aid, 'Home', 'Product', 'index', "sid={$this->aid}", true);
		$this->assign('product', $product);
		$this->display();
	}
	
	// 微信回复设置
	public function wechatAction() {
		$wechat_rule = '__product_rule__';
		$d = D('WechatReply');
		$reply = $d->getReply($this->aid, $wechat_rule);
		if (!$reply) {
			$url = url('Home', 'Product', 'index', 'sid=' . $this->aid, true);
			$id = $d->setKeywordReply($this->aid, $wechat_rule, '微商城', '微商城', '点击进入【微商城】', '', $url);
			$id && $reply = $d->find($id);
		}
		$reply['keyword'] = trim($reply['keyword'], ' ,');
		$this->assign('reply', $reply);
		$this->display();
	}
	
	// 提交微信回复
	public function wechatPostAction() {
		$d = D('WechatReply');
		if ($d->create()) {
			$d->keyword = $d->parseKeyword($d->keyword);
			if ($d->save()) {
				$this->ajaxSuccess();
			}
		}
		$this->ajaxError();
	}
	
	// 保存商品
	public function saveAction() {
		$dProduct = D('Product');
		if ($dProduct->create()) {
			$dProduct->cid = $this->aid;
			if (empty($dProduct->id)) {
				C('TOKEN_ON', true);
				$dProduct->id = $dProduct->cid;
				$dProduct->add() !== false && $this->ajaxSuccess();
			} else {
				/* 配置保存 */
				unset($dProduct->cid);
				$config = $_REQUEST['config'];
				$newProduct = clone $dProduct;
				$product = $newProduct->find( $dProduct->id );
				$product['config'] = unserialize($product['config']);
				foreach ($product['config'] as $k => &$v) {
					if( isset($config[$v['name']]) ) {
						$v['value'] = $config[$v['name']];
					}
				}
				$dProduct->config = serialize($product['config']);
 				/* 保存 */
				$dProduct->save() !== false && $this->ajaxSuccess();
			}
		}
		$this->ajaxError($dProduct->getError());
	}
	
	// 商城配置
	public function configAction() {
		$dProduct = D('Product');
		$product = $dProduct->find($this->aid);
		if ( $product ) {
			$product['config'] = unserialize( $product['config'] );
			foreach ($product['config'] as $k => &$v) { // 生成html
				if ( $v['type'] == 'radio' ) {
					$v['html'] = $this->getRadio('config['.$v['name'].']', $v['data'], $v['value'] );
				}
			}
		}
		$this->assign("product", $product);
		$this->display();
	}
}